package com.example.howard.xiao7music.model;

import android.content.Context;

import com.example.howard.xiao7music.model.getbasedata.GetMusicData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Howard on 2016/9/24.
 */
public class GetLocalSource implements GetResource.GetLocalSource {

    //查找所有歌手
    @Override
    public List<Map<String, Object>> getAllLocalSource(Context context) {
        List<Map<String, Object>> allMusicData;
        allMusicData = GetMusicData.GetMusicbyName(context);
        return allMusicData;
    }
    //查找所有歌手
    @Override
    public List<String> getSingerName(Context context) {
        List<Map<String, Object>> allMusicData;
        List<String> singernamelist = new ArrayList<>();
        Map<String, Object> map;
        Object object;
        String singername;

        allMusicData = getAllLocalSource(context);

        int i;
        for (i = 0; i < allMusicData.size(); i++) {
            map = allMusicData.get(i);
            object = map.get("SINGER");
            singername = object.toString();

            if (i == 0) {
                singernamelist.add(singername);

            } else if (!singernamelist.contains(singername)) {
                singernamelist.add(singername);
            }
        }
        return singernamelist;
    }
    //查找所有文件夹名称
    @Override
    public List<Map<String, Object>> getFolderName(Context context) {
        List<Map<String, Object>> allMusicData;
        List<Map<String,Object>> folderPathAndName=new ArrayList<>();
        List<String> folderPath=new ArrayList<>();
        List<String> folderName=new ArrayList<>();
        Map<String,Object> map;
        Map<String,Object> foldermap =new HashMap<>();
        Object objectURL,objectName;
        String path,name,allPath,foldername;

        allMusicData = getAllLocalSource(context);

        int i;
        for (i=0;i<allMusicData.size();i++){
            map=allMusicData.get(i);
            objectURL=map.get("URL");
            objectName=map.get("NAME");
            name=objectName.toString();
            allPath=objectURL.toString();
            path=allPath.substring(19,allPath.length()-name.length()-1);
            foldername=path.substring(path.lastIndexOf('/')+1);
            if (i==0){
                foldermap =new HashMap<>();
                foldermap.put("FOLDERNAME",foldername);
                foldermap.put("PATH",path);
                folderPathAndName.add(foldermap);
                folderPath.add(path);
                folderName.add(foldername);
            }else if (!folderPath.contains(path)){
                foldermap =new HashMap<>();
                foldermap.put("FOLDERNAME",foldername);
                foldermap.put("PATH",path);
                folderPathAndName.add(foldermap);
                folderPath.add(path);
                folderName.add(foldername);
            }
        }
        return folderPathAndName;
    }
    //查找歌手歌曲
    @Override
    public List<Map<String, Object>> getLocalSourceBySinger(Context context, String singer) {
        List<Map<String, Object>> allMusicData;
        List<Map<String, Object>> songlistbysinger = new ArrayList<>();
        Map<String, Object> map;

        allMusicData = getAllLocalSource(context);
        int i;
        for (i = 0; i < allMusicData.size(); i++) {
            map = allMusicData.get(i);

            if (map.get("SINGER").toString().equals(singer)) {
                songlistbysinger.add(map);
            }
        }
        return songlistbysinger;
    }
    //查找文件夹内歌曲
    @Override
    public List<Map<String, Object>> getLocalSourceByFolder(Context context, String path) {
        List<Map<String, Object>> allMusicData;
        List<Map<String,Object>> songlistbyfolder=new ArrayList<>();
        Map<String,Object> map;
        String folderPath,songName,folderAllPath;
        Object objectURL,objectName;
//        String path=foldermap.get("PATH").toString();

        allMusicData = getAllLocalSource(context);
        int i;
        for (i=0;i<allMusicData.size();i++) {
            map=allMusicData.get(i);
            objectURL=map.get("URL");
            objectName=map.get("NAME");
            songName=objectName.toString();
            folderAllPath=objectURL.toString();
            folderPath=folderAllPath.substring(19,folderAllPath.length()-songName.length()-1);

            if (folderPath.equals(path)){
                songlistbyfolder.add(map);
            }
        }
        return songlistbyfolder;
    }

}
